var express = require('express');
var router = express.Router();
const query = require('../../utils/mysql');
const Result = require('../../utils/api');

//文章分页查询
router.post('/view/article/list', async function (req, res, next) {

    let sql = 'SELECT a.id,a.article_title,a.article_pic,a.article_content,a.add_time,a.is_top,a.visit,a.type,b.id as cid,b.cate_name FROM blog_article as a RIGHT JOIN	blog_category as b ON a.cid = b.id WHERE a.is_del = ? AND a.is_publish = ? LIMIT ?,?'
    let sqlArr = [0, 1, (req.body.page - 1) * req.body.limit, req.body.limit]

    let data = await query(sql, sqlArr)
    let dataArr = Array.from(data)

    Result.success(res, dataArr)

})

// 文章详情
router.get('/view/blog/detail/:id', async function (req, res, next) {
    let id = req.params.id
    let sql = 'SELECT a.id,a.article_title,a.article_pic,a.article_content,a.add_time,a.is_top,a.visit,a.type,b.id as cid,b.cate_name FROM blog_article as a RIGHT JOIN	blog_category as b ON a.cid = b.id WHERE a.is_publish = ? AND a.is_del = ? AND a.id = ?'
    let sqlArr = [1, 0, id]

    let tagSql = 'SELECT * FROM blog_article_tag WHERE aid = ?'
    let tagArr = [id]

    let datas = await query(tagSql, tagArr)

    // console.log(datas, "DATAS")
    let dataMap = datas.map(current => current.tid)
    let arr = await Promise.all(dataMap.map(async current => {
        let sql = 'SELECT tag_name FROM blog_tag WHERE id = ?'
        let sqlArr = [current]
        let [tag] = await query(sql, sqlArr)
        return tag.tag_name
    }))
    // console.log(arr, "ARR")
    let [data] = await query(sql, sqlArr)

    let resData = Object.assign(data, {
        tags: arr
    })

    Result.success(res, resData)
    // res.json(arr)
})

// 文章分类
router.get('/view/blog/cates', async function (req, res, next) {
    let sql = 'SELECT id,cate_name FROM blog_category WHERE is_del = ? AND is_show = ?'
    let arr = [0, 1]
    let cates = await query(sql, arr)
    let count = Array.from(cates).length

    let cateArr = cates.map(async current => {
        let sql = 'SELECT COUNT(*) AS count FROM blog_article WHERE cid = ?'
        let sqlArr = [current.id]
        let count = await query(sql, sqlArr)
        return Object.assign(current, {
            count: count[0].count
        })
    })
    let data = await Promise.all(cateArr)
    Result.success(res, {
        data: data,
        count: count
    })
})

// 文章标签
router.get('/view/blog/tags', async function (req, res, next) {

    let sql = 'SELECT id,tag_name FROM blog_tag WHERE is_del = ? AND is_show = ?'
    let arr = [0, 1]
    let cates = await query(sql, arr)
    let count = Array.from(cates).length

    let cateArr = cates.map(async current => {
        let sql = 'SELECT COUNT(*) AS count FROM blog_article_tag WHERE tid = ?'
        let sqlArr = [current.id]
        let count = await query(sql, sqlArr)
        return Object.assign(current, {
            count: count[0].count
        })
    })
    let data = await Promise.all(cateArr)
    Result.success(res, {
        data: data,
        count: count
    })
})

module.exports = router;